﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace shopform
{
    /// <summary>
    /// Executes SQL commands on myDatabaseConnection.
    /// </summary>
    public static class myDatabaseCommander
    {

        /// <summary>
        /// Executes a 'non-returning' SQL Query.
        /// Throws SqlException if problem occured, needs to be 
        /// caught by caller.
        /// </summary>
        /// <param name="sqlQueryString">SQL command to be executed.</param>
        public static void ExecuteNonQuery(String sqlQueryString)
        {
            try
            {
                SqlCommand command = new SqlCommand();
                command.Connection = myDatabaseConnection.getConnection();
                command.CommandText = sqlQueryString;
                command.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Returns a single, non-array result from an sql command execution.
        /// </summary>
        /// <param name="sqlQueryString">SQL command to be executed.</param>
        /// <returns>Typecasting(explicit type conversion) to desired type required.</returns>
        public static object ExecuteScalar(String sqlQueryString)
        {
            try
            {
                SqlCommand command = new SqlCommand();
                command.Connection = myDatabaseConnection.getConnection();
                command.CommandText = sqlQueryString;
                object retObj = command.ExecuteScalar();
                return retObj;
            }
            catch (SqlException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Executes a query resulting in a multirow-multicolumn response.
        /// </summary>
        /// <returns>A DataTable populated with the result of the sqlQueryString.</returns>
        public static DataTable ExecuteArray(String sqlQueryString)
        {
            try
            {
                SqlCommand command = new SqlCommand();
                command.Connection = myDatabaseConnection.getConnection();
                command.CommandText = sqlQueryString;
                SqlDataReader reader = command.ExecuteReader();
                DataTable table = new DataTable();
                table.Load(reader);

                return table;
            }
            catch(SqlException e)
            {
                throw e;
            }
        }
        
    }//end of class
}
